package org.nzj.useradmin.junit;

import java.util.Iterator;
import java.util.List;

import org.junit.BeforeClass;
import org.junit.Test;
import org.nzj.useradmin.dao.IUserDAO;
import org.nzj.useradmin.util.DateFormatTest;
import org.nzj.useradmin.vo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class IUserDAOImplTest {
	/*
	 * 
	 * $Proxy8 $Proxy7 
	 * 异常 是代理类转换出错。
	 * 代理的类是根据接口来进行的，
	 * 所以在把代理类转换成IUserDAOImpl时得用IUserDAOImpl的接口IUserDAO来接收
	 */
	private static IUserDAO userDao;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		try {
			// applicationContext.xml放在WEB-INF目录不能用ClassPathXmlApplicationContext实例，只能用下面的
			ApplicationContext ctx = new FileSystemXmlApplicationContext(
					"WebRoot/WEB-INF/applicationContext.xml");
			// ApplicationContext ctx = new
			// ClassPathXmlApplicationContext("applicationContext.xml");
			userDao = (IUserDAO) ctx.getBean("iUserDAOImpl");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void save() throws Exception {
		
		System.out.println(userDao.doCreate(new User("No123", "牛志杰", "12345")));
	}
	@Test
	public void delete() throws Exception{
		System.out.println(userDao.doDelete(66));
	}
	@Test
	public void doIf() throws Exception{
		System.out.println(userDao.doIf("No120"));
	}
	@Test
	public void doUpdate() throws Exception{
			User user=new User();
			user.setId(96);
			user.setUserno("No116");
			user.setName("李长久");
			user.setPasswd("12345");
			user.setSex("男");
			user.setBirthday(DateFormatTest.getDateFromString("1981/02/12", "yyyy/mm/dd"));
			user.setEmail("nzj1981@126.com");
			user.setPic("images/aa.gif");
			user.setIp("192.168.1.101");
		System.out.println(userDao.doUpdate(user));
	}
	@Test
	public void findAll() throws Exception{
		long starTime=System.currentTimeMillis();
		List<User> list=userDao.findAll("张", 0, 1);
	   for (Iterator<User> i=list.iterator();i.hasNext();) {
		 User user=i.next();
		 System.out.println(user);
	}
		long endTime=System.currentTimeMillis();
		System.out.println((endTime-starTime)+"毫秒"); 
	}
	@Test
	public void findById() throws Exception{
		System.out.println(userDao.findById(100));
	}
	@Test
	public void findByUserNO() throws Exception{
		System.out.println(userDao.findByUserNO("No120"));
	}
	@Test
	public void findLogin() throws Exception{
		User user = new User();
		user.setUserno("");
		user.setPasswd("");
		boolean flag=userDao.findLogin(user);
		String name=user.getName();
		System.out.println(flag+"----"+name);
	}
	@Test
	public void getAllCount() throws Exception{
		long starTime=System.currentTimeMillis();
		System.out.println(userDao.getAllCount("张 "));
		long endTime=System.currentTimeMillis();
		System.out.println((endTime-starTime)+"毫秒"); 
	}
}
